package com.alan.libapi.api.Impl;

import com.alan.libapi.api.ALTcpClient;
import com.alan.libapi.api.ApiResponse;
import com.alan.libapi.api.ApiTcp;
import com.alan.libapi.api.IALTcpClient;
import com.alan.libapi.model.business.ConnectedDeviceInfo;
import com.alan.libapi.model.business.SharedWifiInfo;
import com.alan.libapi.model.business.TcpJsonResponseError;
import com.alan.libapi.model.business.TcpJsonResponseOfGetDeviceUserList;
import com.alan.libapi.model.business.TcpJsonResponseOfStatus;
import com.alan.libapi.model.business.TcpResponseStatus;
import com.alan.libapi.model.persistent.ErrorInfo;
import com.alan.library.allog.KLog;
import com.alibaba.fastjson.JSON;

import java.io.IOException;
import java.util.List;

/**
 * @className: ApiTcpImpl
 * @describtion: ApiTcpImpl
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/28 18:34
 */
public class ApiTcpImpl implements ApiTcp, IALTcpClient {

    private ALTcpClient alTcpClient;

    public ApiTcpImpl(ALTcpClient alTcpClient) {
        this.alTcpClient = alTcpClient;
    }

    private ApiResponse<TcpResponseStatus> parseResponseForStatus(String responseString) {
        TcpJsonResponseOfStatus tcpJsonResponseOfStatus = JSON.parseObject(responseString, TcpJsonResponseOfStatus.class);
        ApiResponse<TcpResponseStatus> apiResponse = new ApiResponse<>();
        if(tcpJsonResponseOfStatus != null){
            if(tcpJsonResponseOfStatus.getResult() != null){
                apiResponse.setRetCode(ApiResponse.SUCCESS_CODE);
                apiResponse.setResult(tcpJsonResponseOfStatus.getResult());
            }else {
                apiResponse.setRetCode(ApiResponse.DEFAULT_ERROR_CODE);
                TcpJsonResponseError tcpJsonResponseError = JSON.parseObject(responseString, TcpJsonResponseError.class);
                apiResponse.setErrorResponse(new ErrorInfo(tcpJsonResponseError.getError().getCode(), tcpJsonResponseError.getError().getDebugMsg()));
            }
        }else {
            apiResponse.setResult(null);
        }
        return apiResponse;
    }

    @Override
    public ApiResponse<TcpResponseStatus> deleteDeviceUser(int deviceMasterId, String userMac) {
        String responseString = alTcpClient.request(JsonRequestHelper.getJsonStrOfDeleteDeviceUser(deviceMasterId, userMac));
        return parseResponseForStatus(responseString);
    }

    @Override
    public ApiResponse<TcpResponseStatus> sendWifiInfo(int deviceMasterId, List<SharedWifiInfo> wifiInfoList) {
        String responseString = alTcpClient.request(JsonRequestHelper.getJsonStrOfSendWifiInfo(deviceMasterId, wifiInfoList));
        return parseResponseForStatus(responseString);
    }

    @Override
    public ApiResponse<TcpResponseStatus> modifyDeviceWifi(int deviceMasterId, String SSID, String password) {
        String responseString = alTcpClient.request(JsonRequestHelper.getJsonStrOfModifyDeviceWifi(deviceMasterId, SSID, password));
        return parseResponseForStatus(responseString);
    }

    @Override
    public ApiResponse<List<ConnectedDeviceInfo>> getDeviceUserList(int deviceMasterId) {
        String responseString = alTcpClient.request(JsonRequestHelper.getJsonStrOfGetDeviceUserList(deviceMasterId));
        TcpJsonResponseOfGetDeviceUserList tcpJsonResponseOfGetDeviceUserList = JSON.parseObject(responseString, TcpJsonResponseOfGetDeviceUserList.class);
        ApiResponse<List<ConnectedDeviceInfo>> apiResponse = new ApiResponse<>();
        if(tcpJsonResponseOfGetDeviceUserList != null){
            if(tcpJsonResponseOfGetDeviceUserList.getResult() != null){
                apiResponse.setRetCode(ApiResponse.SUCCESS_CODE);
                apiResponse.setResult(tcpJsonResponseOfGetDeviceUserList.getResult().getDeviceUserList());
            }else {
                apiResponse.setRetCode(ApiResponse.DEFAULT_ERROR_CODE);
                TcpJsonResponseError tcpJsonResponseError = JSON.parseObject(responseString, TcpJsonResponseError.class);
                apiResponse.setErrorResponse(new ErrorInfo(tcpJsonResponseError.getError().getCode(), tcpJsonResponseError.getError().getDebugMsg()));
            }
        }else {
            apiResponse.setResult(null);
        }
        return apiResponse;
    }

    @Override
    public ApiResponse<TcpResponseStatus> sendMasterInfo(int deviceMasterId) {
        String responseString = alTcpClient.request(JsonRequestHelper.getJsonStrOfSendMasterInfo(deviceMasterId));
        return parseResponseForStatus(responseString);
    }

    @Override
    public boolean isAlive() {
        return alTcpClient.isAlive();
    }

    @Override
    public void retryConnection() throws IOException {
        alTcpClient.retryConnection();
    }

    @Override
    public void close() throws IOException {
        alTcpClient.close();
    }
}
